﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tst.Models;
using Ext.Direct.Mvc;

namespace Tst.Controllers
{
    public class PlanoTesteController : Controller
    {
        private PlanoTesteRepository planoTesteRep;
        private ProjetoRepository projetoRep;
        private ModuloRepository moduloRep;

        public PlanoTesteController()
        {
            this.planoTesteRep = new PlanoTesteRepository();
            this.projetoRep = new ProjetoRepository();
            this.moduloRep = new ModuloRepository();
        }


        public ActionResult List()
        {            
            IList <PlanoTeste> planos = this.planoTesteRep.GetAll();
            IList<object> json = new List<object>();

            foreach(PlanoTeste plano in planos)
            {                
                plano.ProjetoReference.Load();
                plano.ModuloReference.Load();

                json.Add(new { 
                     cd_plano_teste = plano.cd_plano_teste
                    ,cd_projeto = plano.Projeto.cd_projeto
                    ,cd_modulo = plano.Modulo.cd_modulo
                    ,ds_plano_teste = plano.ds_descricao
                    ,ds_projeto = plano.Projeto.ds_descricao
                    ,ds_modulo = plano.Modulo.ds_descricao
                    ,ds_criterios_termino = plano.ds_criterios_termino
                    ,ds_documentos = plano.ds_documentos
                    ,ds_func_afetadas = plano.ds_func_afetadas
                    ,ds_riscos = plano.ds_riscos
                });
            }
            
            return this.Direct(json);            
        }

        [FormHandler]
        public ActionResult Create(FormCollection request)
        {
            PlanoTeste planoTeste = this.planoTesteRep.GetByDescricao(request.GetString("ds_descricao"));

            if (planoTeste != null && planoTeste.cd_plano_teste != request.GetInteger("cd_plano_teste"))
            {
                return this.Direct(new
                {
                    success = false,
                    errors = new
                    {
                        ds_plano_teste = "Plano de Teste já cadastrado."
                    }
                });
            }

            if (String.IsNullOrEmpty(request["cd_plano_teste"]))
            {
                planoTeste = new PlanoTeste()
                {
                    ds_descricao = request.GetString("ds_descricao"),
                    ds_criterios_termino = request.GetString("ds_criterios_termino"),
                    ds_documentos = request.GetString("ds_documentos"),
                    ds_func_afetadas = request.GetString("ds_func_afetadas"),
                    ds_riscos = request.GetString("ds_riscos"),
                    Projeto = projetoRep.GetById(request.GetInteger("cd_projeto")),
                    Modulo = moduloRep.GetById(request.GetInteger("cd_modulo"))
                    
                };

                this.planoTesteRep.Insert(planoTeste);
            }
            else
            {
                planoTeste = this.planoTesteRep.GetById(Convert.ToInt16(request["cd_plano_teste"]));
                planoTeste.ModuloReference.Load();

                if (planoTeste.Modulo.cd_modulo != request.GetInteger("cd_modulo"))
                {
                    planoTeste.Modulo = moduloRep.GetById(request.GetInteger("cd_modulo"));               
                }

                planoTeste.ds_descricao = request.GetString("ds_descricao");
                planoTeste.ds_criterios_termino = request.GetString("ds_criterios_termino");
                planoTeste.ds_documentos = request.GetString("ds_documentos");
                planoTeste.ds_func_afetadas = request.GetString("ds_func_afetadas");
                planoTeste.ds_riscos = request.GetString("ds_riscos");



                this.planoTesteRep.Update(planoTeste);
            }

            return this.Direct(new
            {
                success = true,
                cd_plano_teste = planoTeste.cd_plano_teste

            });
        }

        public ActionResult Delete(int cd_plano_teste)
        {
            this.planoTesteRep.Delete(cd_plano_teste);
            this.planoTesteRep.Save();

            return this.Direct(new { success = true });
        }
    }
}
